package com.gjy.sql.oracle.newland.finance;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.google.common.collect.Lists;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-07-14 16:18:11
 */
public class FinancialWriter {

    public void writeMore(String writePath, List<DataSheet> dataSheet, List<CheckSheet> checkSheets, SumSheet sumSheet, SumCheckSheet sumCheckSheet) {
        try (ExcelWriter writer = EasyExcel.write(writePath).registerWriteHandler(unifiedStyle2())
                .registerWriteHandler(new AutoColumnWidthStrategy()).build()) {

            AtomicInteger offset = new AtomicInteger();
            for (DataSheet ds : dataSheet) {
                WriteSheet sheet = EasyExcel.writerSheet("数据文件").needHead(false).build();

                WriteTable wt0 = EasyExcel.writerTable(offset.getAndIncrement()).head(DataSheet.class).needHead(Boolean.TRUE).build();
                List<DataSheet> l = new ArrayList<>();
                l.add(new DataSheet(ds.getFileName()));
                l.add(new DataSheet("文件内容："));
                writer.write(l, sheet, wt0);

                WriteTable wt1 = EasyExcel.writerTable(offset.getAndIncrement()).head(FinancialData.class).needHead(Boolean.TRUE).build();
                writer.write(ds.getContent(), sheet, wt1);

                writer.write(Collections.singleton(""), sheet, EasyExcel.writerTable().build());
            }

            for (CheckSheet ds : checkSheets) {
                WriteSheet sheet = EasyExcel.writerSheet("校验文件").needHead(false).build();

                WriteTable wt0 = EasyExcel.writerTable(offset.getAndIncrement()).head(CheckSheet.class).needHead(Boolean.TRUE).build();
                List<CheckSheet> l = new ArrayList<>();
                l.add(new CheckSheet(ds.getFileName()));
                l.add(new CheckSheet("文件内容："));
                writer.write(l, sheet, wt0);

                WriteTable wt1 = EasyExcel.writerTable(offset.getAndIncrement()).head(CheckData.class).needHead(Boolean.TRUE).build();
                List<CheckData> l2 = new ArrayList<>();
                l2.add(ds.getCheckData());
                writer.write(l2, sheet, wt1);

                writer.write(Lists.newArrayList("", ""), sheet, EasyExcel.writerTable().build());
            }


            WriteSheet sheet = EasyExcel.writerSheet("汇总数据文件").needHead(false).build();

            WriteTable wt0 = EasyExcel.writerTable(offset.getAndIncrement()).head(SumSheet.class).needHead(Boolean.TRUE).build();
            List<SumSheet> l = new ArrayList<>();
            l.add(new SumSheet(sumSheet.getFileName()));
            l.add(new SumSheet("文件内容："));
            writer.write(l, sheet, wt0);

            WriteTable wt1 = EasyExcel.writerTable(offset.getAndIncrement()).head(SumData.class).needHead(Boolean.TRUE).build();
            writer.write(sumSheet.getSumDataList(), sheet, wt1);


            WriteSheet sheet2 = EasyExcel.writerSheet("汇总校验文件").needHead(false).build();

            WriteTable wt2 = EasyExcel.writerTable(offset.getAndIncrement()).head(SumCheckSheet.class).needHead(Boolean.TRUE).build();
            List<SumSheet> l2 = new ArrayList<>();
            l2.add(new SumSheet(sumCheckSheet.getFileName()));
            l2.add(new SumSheet("文件内容："));
            writer.write(l, sheet2, wt2);

            WriteTable wt3 = EasyExcel.writerTable(offset.getAndIncrement()).head(SumCheckData.class).needHead(Boolean.TRUE).build();
            List<SumCheckData> data = new ArrayList<>();
            data.add(sumCheckSheet.getSumCheckData());
            writer.write(data, sheet2, wt3);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static HorizontalCellStyleStrategy unifiedStyle2() {
        // 共享样式（表头与内容相同）
        WriteCellStyle sharedStyle = new WriteCellStyle();
        sharedStyle.setVerticalAlignment(VerticalAlignment.CENTER);    // 垂直居中
        sharedStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);  // 左对齐

        // 关键配置：清除表头修饰
        sharedStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex()); // 白色背景覆盖黑色
        sharedStyle.setWrapped(false); // 关闭自动换行
        WriteFont font = new WriteFont();
        font.setFontName("宋体");
        font.setBold(false); // 取消加粗
        font.setFontHeightInPoints((short) 11);
        sharedStyle.setWriteFont(font);

        // 边框样式（可选）
        sharedStyle.setBorderTop(BorderStyle.THIN);
        sharedStyle.setBorderBottom(BorderStyle.THIN);

        // 表头与内容使用相同样式
        return new HorizontalCellStyleStrategy(sharedStyle, sharedStyle);
    }

    // 1. 定义统一样式（表头与内容相同）
    public static HorizontalCellStyleStrategy unifiedStyle() {
        WriteCellStyle sharedStyle = new WriteCellStyle();
        // 基础样式配置
        sharedStyle.setVerticalAlignment(VerticalAlignment.CENTER);    // 垂直居中
        sharedStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);  // 左对齐
        sharedStyle.setWrapped(true);                                  // 自动换行

        // 边框样式（细黑边框）
        sharedStyle.setBorderTop(BorderStyle.THIN);
        sharedStyle.setBorderBottom(BorderStyle.THIN);
        sharedStyle.setBorderLeft(BorderStyle.THIN);
        sharedStyle.setBorderRight(BorderStyle.THIN);
        sharedStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
        sharedStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());

        // 字体（宋体，常规）
        WriteFont font = new WriteFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 11);
        sharedStyle.setWriteFont(font);

        // 表头与内容使用相同样式
        return new HorizontalCellStyleStrategy(sharedStyle, sharedStyle);
    }

    public void fillTemplate(String templatePath, String writePath, List<DataSheet> list) {
        try (ExcelWriter writer = EasyExcel.write(writePath).withTemplate(templatePath).build()) {

            int offset = 2;

            for (DataSheet ds : list) {
                WriteSheet sheet = EasyExcel.writerSheet("数据文件").relativeHeadRowIndex(offset).build();

                Map<String, Object> fileNameMap = new HashMap<>();
                fileNameMap.put("fileName", ds.getFileName());
                FillConfig build = FillConfig.builder().forceNewRow(true).build();
                writer.fill(fileNameMap, build, sheet);

                WriteSheet sheet2 = EasyExcel.writerSheet("数据文件").relativeHeadRowIndex(offset + 3).build();
                FillWrapper contentWrapper = new FillWrapper("content", ds.getContent());
                writer.fill(contentWrapper, build, sheet2);

                int size = ds.getContent().size();
                offset += (size + offset + 2);

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
